{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from als import *\n",
    "\n",
    "ratings = load_data('../ml-latest-small/ratings.csv', headers=True)[:50, :50]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration #0\n",
      "iteration #1\n",
      "iteration #2\n",
      "iteration #3\n",
      "iteration #4\n",
      "iteration #5\n",
      "iteration #6\n",
      "iteration #7\n",
      "iteration #8\n",
      "iteration #9\n"
     ]
    }
   ],
   "source": [
    "interations = 10\n",
    "\n",
    "bath_als = BatchALS(ratings=ratings, rank=4)\n",
    "\n",
    "n_users, n_ratings = ratings.shape\n",
    "\n",
    "P, Q = BatchALS.random_factors(n_users, n_ratings, 4)\n",
    "\n",
    "for i in range(interations):\n",
    "    P, Q = bath_als.run(P, Q)\n",
    "    print('iteration #{}'.format(i))\n",
    "\n",
    "nR = np.dot(P, Q.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.3584223196535892\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "indices = ratings.nonzero()\n",
    "\n",
    "s = []\n",
    "for i in indices[0]:\n",
    "    for j in indices[1]:\n",
    "        s.append((ratings[i, j] - nR[i][j]) ** 2)\n",
    "\n",
    "print(np.mean(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
